/*
 * Proxy.cpp
 *
 *  Created on: Dec 24, 2011
 *      Author: quotrader
 */

#include "Proxy.h"
#include <pthread.h>
#include <sstream>

Proxy::Proxy() {
	Scheduler::Scheduler();
	test=0;
}

void Proxy::execute() {

	Task *tasks = this->tMng->tasks;
	int numTask = this->tMng->numTask;
	stringstream tmp;
	string str;


	// send tasks to algorithm in order to sort and play with them
	this->algr->run(this->tMng);

	//start the scale
	this->scale->start();

	// create the controller thread (proxy)
	pthread_create(&this->ctrThread,NULL, &Proxy::control, this);
	pthread_setname_np(this->ctrThread, "CTR_T");

	// create task's threads
	for (int i = 0; i < numTask; i++) {
		//name threads
		tmp << "THREAD#" << tasks[i].id;
		str = tmp.str();
		pthread_create(&tasks[i].taskThread, NULL, &Task::run,&tasks[i]);
		pthread_setname_np(tasks[i].taskThread, str.c_str());
		tmp.str("");
	}
}
